package com.zut.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zut.constant.MessageConstant;
import com.zut.entity.Result;
import com.zut.service.UserService;
import org.omg.IOP.ServiceContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sq
 * @create 2021-07-17-17:01
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getUsername")
    //获取当前登录的用户名
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象中，底层是用session实现的
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null) {
            //查询失败
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        } else {
            //获取当前登录的用户名
            String username = user.getUsername();
            //查询成功
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }
    }
}
